package com.newgoldcurrency.activities;

import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.newgoldcurrency.App;
import com.newgoldcurrency.R;
import com.newgoldcurrency.databinding.ActivityInputNameBinding;
import com.newgoldcurrency.utils.CancelCall;
import com.newgoldcurrency.utils.Tools;
import com.newgoldcurrency.utils.network.Network;

import java.io.IOException;
import java.util.HashMap;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class InputNameActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityInputNameBinding binding = ActivityInputNameBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.inputNameBack.setOnClickListener(view -> finish());
        binding.inputName.setText(Tools.checkTapHere(this, view -> {
            if (binding.inputNameMiddle.getVisibility()==View.GONE){
                binding.inputNameMiddle.setVisibility(View.VISIBLE);
                binding.inputNameMaiden.setVisibility(View.VISIBLE);
            }else {
                binding.inputNameMiddle.setVisibility(View.GONE);
                binding.inputNameMaiden.setVisibility(View.GONE);
            }
        }));
        binding.inputName2.setText(Tools.checkTapHere(this, view -> {
            if (binding.inputNameMiddle2.getVisibility()==View.GONE){
                binding.inputNameMiddle2.setVisibility(View.VISIBLE);
                binding.inputNameMaiden2.setVisibility(View.VISIBLE);
            }else {
                binding.inputNameMiddle2.setVisibility(View.GONE);
                binding.inputNameMaiden2.setVisibility(View.GONE);
            }
        }));
        binding.inputName.setMovementMethod(LinkMovementMethod.getInstance());
        binding.inputName2.setMovementMethod(LinkMovementMethod.getInstance());
        binding.inputNameSave.setOnClickListener(view -> Tools.dialog_input_name(this, view1 -> {
            String f_e = binding.inputNameFirstNameEn.getText().toString();
            String l_e = binding.inputNameLastNameEn.getText().toString();
            String mid_e = binding.inputNameMiddleNameEn.getText().toString();
            String maid_e = binding.inputNameMaidenNameEn.getText().toString();

            String f_d = binding.inputNameFirstNameDoc.getText().toString();
            String l_d = binding.inputNameLastNameDoc.getText().toString();
            String mid_d = binding.inputNameMiddleNameDoc.getText().toString();
            String maid_d = binding.inputNameMaidenNameDoc.getText().toString();
            if (f_d.isEmpty()||f_e.isEmpty()){
                Tools.toast(getString(R.string.first_name_not_empty));
                return;
            }
            HashMap<String, String> map=new HashMap<>();
            map.put("user_id", App.userBean.id);
            map.put("english_first_name",f_e);
            map.put("english_middle_name",mid_e);
            map.put("english_last_name",l_e);
            map.put("english_maiden_name",maid_e);

            map.put("documentation_first_name",f_d);
            map.put("documentation_middle_name",mid_d);
            map.put("documentation_last_name",l_d);
            map.put("documentation_maiden_name",maid_d);
            Network.post(map, "user/accountAuthentication", (CancelCall) call -> call.enqueue(new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    e.printStackTrace();
                }
                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) {
                    runOnUiThread(() -> {
                        try {
                            JsonObject jo = JsonParser.parseString(Objects.requireNonNull(response.body()).string()).getAsJsonObject();
                            if (jo.get("message").getAsString().equals("Success")){
                                setResult(3);
                                finish();
                            }else {
                                Tools.toast(jo.get("message").getAsString());
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
                }
            }));
        }));
    }
}